// var floorsData = [] //装楼层按钮数据
var floorsData = []
var pLevel = 20
var floorPathToggle =false;
var realTime = '' //路径需要的时间
var msgLong; //msg的字段长度
var pathFlag = false;
var nowFloorId; //模拟导航内调用 拿到后台请求的当前所在的楼层   模拟导航方法内的regNavigationWalk方法内 调用一次nowFloorId = walkDetail.floorIDs;   取消模拟导航方法开头 调用一次$('.floorToggle').html("" + object.allFoors[nowFloorId] + "");
var arrT = []; //导航状态下  左侧按钮的楼层遍历出来楼层号
var isFloorToggleCounter = false; //楼层切换计数器开关，暂时用于处理当同层不同区导航时暂停后跳转的bug的属性   onSelected方法内的regNavigationPause方法内 调用一次isFloorToggleCounter = true; 继续模拟导航方法尾部 调用一次isFloorToggleCounter = false;
var floorCounter = 0; //楼层切换计数器   继续模拟导航方法尾部 调用一次floorCounter = 0;
var object = {
  startPP: '', //设置起点时获取的起点楼层号   setStart方法内 调用一次object.startPP = object.allFoors[data.floorIDs] goBack方法开头和startP做相同格式判断
  endPP: '', //设置终点时获取的终点楼层号     setEnd方法内 调用一次object.endPP = object.allFoors[data.floorIDs] goBack方法开头和endP做相同格式判断
}
// 切换楼层模块主按钮点击事件
$(function () {
  $(".floorToggle").on("click", function () {
    if (outDivValue) {
      setTimeout(() => {
        FloorDetection()
        myswiper(floorDataRe).init()
      }, 0);
    }
    $(".changeFBox").show().animate({
      height: "4.59rem"
    })
    $(".floorToggle").hide()
  })
})

var outDivValue

function FloorDetection() {
  outDivValue = $(".floorToggle").html()
  $(".swiper-wrapper1 li button").each(function (i, e) {
    if (outDivValue == $(e).val()) {
      // object.no = i
      $(".swiper-wrapper1 li").eq(i).click()
    }
  })
}

// 根据后台返回的数据  获取当前的楼层
function floorJudgment() {
  $("#floorToggleUl_List button").on("click", function () {
    arrT.push($(this).html().toString())
  })
  $(".floorToggleMinUl #floorToggleUl_List button").click()
  // console.log(arrT)
}
/* 获取楼层列表index */
function searchKeys(needle, haystack) {
  for (i in haystack) {
    if (haystack[i] == needle) {
      if (i.length = 1) {
        // fIndex = i
        return i
      }
    }
  }
}

// 出路线方法   在设置起点setStart 终点setEnd  起点终点交换reverse等地方调用
function pathGo(type) {
  $('.pathInfo').hide()
  $('.floorToggleMinUl').children().remove()
  if (object.startP && object.endP) {
    if (object.startP == object.endP) {
      $(".searchPathFailMsg").html("此处为您当前位置,请重新设置")
      $(".searchPathFailBox").fadeIn()
      return
    } else {

      floorsData = []
      floorPathToggle=false;
      $(".floorToggleMinUl").show()

      self.map.searchPath(function (msg) {
        if (msg.type == 'done') {

          $('.mogo').css("background-image", "url('./images/mogo.png')");
          $('.mogo').text('点击进行模拟导航');
          pathFlag = true;

          //遍历楼层数据
          $(".swiper-wrapper1 li button").each(function (i) {
            var arr = {}
            arr.floorsId = $(this).attr("data")
            arr.floorsNo = $(this).attr("value")
            floorsData.push(arr)
          })

          var li = ''
          var floorIDsRe = msg.floorIDs.reverse()
          // console.log(floorIDsRe);
          for (let i = 0; i < floorIDsRe.length; i++) {
            for (let j = 0; j < floorsData.length; j++) {
              if (floorIDsRe[i] == floorsData[j].floorsId) {
                li = li + '<li  class="floorToggleUl_List" id="floorToggleUl_List">' +
                  '<i class="floorToggleMinUl_i"></i><button name="btn" data="' + floorsData[j].floorsId + '" value="' + floorsData[j].floorsNo + '" id ="' + floorsData[j].floorsNo + '" >' + floorsData[j].floorsNo + '</button>' +
                  '</li>'
              }
            }
          }
          // console.log(li);
          $(".floorToggle,.changeFBox").hide()
          $(".floorToggleMinUl").append(li)
          $(".floorToggleMinUl li").eq(-1).show().removeClass().addClass('floorToggleUl_List-active')
          $(".floorToggleMinUl li").eq(-1).attr('data', 'isMark')
          if (floorIDsRe.length == 1) {} else {
            for (let j = 0; j < floorIDsRe.length; j++) {
              $(".floorToggleMinUl li").eq(j + 1).find('i').show()
            }
            $(".floorToggleMinUl li").eq(-1).find('i').show().removeClass().addClass("floorToggleMinUl_iR")
          }

          qrCodeData = {}
          // console.log(msg)
          $(".clickDetails").fadeIn(()=>{
            setTimeout(() => {
              $('.clickDetails .clickDetailsClose').fadeIn(500,()=>{
                $('.clickDetails .clickDetailsClose span').addClass('clickDetailsCloseFadeOut')
              })
            }, 800);
            
          })
          $('.searchResultsOnMapBox').fadeOut();
          realdistance = Math.ceil(msg.dis / 20)
          realTime = Math.ceil(realdistance / 1.1)
          if (msg.floorIDs.length == 1) {
            $('.pathChoice').hide()
            $('.pathChoiceLast').show()
            $('.clickDetails .pathChoiceLast').find('span').eq(0).html(formatSeconds(realTime) + '&nbsp&nbsp' + '约 ' + realdistance + ' 米')
            $('.clickDetails .pathChoiceLast').find('span').eq(1).html('到达目的地' + ' : ' + $('.clickDetails .opEndP').val())
          } else {
            $('.pathInfo').show()
            $('.pathChoice').show()
            $('.pathChoiceLast').hide()
          }
          self.map.areaClickEmbed = false;
          object.isFind = true;
          self.map.setPolarAngle(0)
          if (type == 'liftsFirst') {
            $(".clickDetails .firstChoice").find("li").eq(1).children().remove()
            $(".clickDetails .firstChoice").find("li").eq(1).html(formatSeconds(realTime))
            $(".clickDetails .firstChoice").find("li").eq(2).children().html('约' + realdistance + '米')
            $(".clickDetails .firstChoice").find("li").addClass("thisLiIsActive")
          } else if (type == 'escalatorsFirst') {
            $(".clickDetails .secondChoice").find("li").eq(1).children().remove()
            $(".clickDetails .secondChoice").find("li").eq(1).html(formatSeconds(realTime))
            $(".clickDetails .secondChoice").find("li").eq(2).children().html('约' + realdistance + '米')
          } else if (type == 'stairsFirst') {
            $(".clickDetails .lastChoice").find("li").eq(1).children().remove()
            $(".clickDetails .lastChoice").find("li").eq(1).html(formatSeconds(realTime))
            $(".clickDetails .lastChoice").find("li").eq(2).children().html('约' + realdistance + '米')
          }
          $(".startEndPointBar").find("span").eq(0).html(realdistance)
          $(".startEndPointBar").find("span").eq(1).html(formatSeconds(realTime))
          if (msg.floorIDs.length == 1) {
            if (msg.dis > 2000) {
              pLevel = 15
              self.map.zoomLevel(pLevel);
            } else if (msg.dis < 2000 && msg.dis > 1500) {
              pLevel = 10
              self.map.zoomLevel(pLevel);
            } else if (msg.dis < 1500 && msg.dis > 1000) {
              pLevel = 7
              self.map.zoomLevel(pLevel)
            } else if (msg.dis < 1000 && msg.dis > 500) {
              pLevel = 5
              self.map.zoomLevel(pLevel)
            } else if (msg.dis < 500) {
              pLevel = 3
              self.map.zoomLevel(pLevel)
            }
          } else {
            if (msg.dis > 2000) {
              pLevel = 10
              self.map.zoomLevel(pLevel);
            } else if (msg.dis < 2000 && msg.dis > 1500) {
              pLevel = 7
              self.map.zoomLevel(pLevel);
            } else if (msg.dis < 1500 && msg.dis > 1000) {
              pLevel = 6
              self.map.zoomLevel(pLevel)
            } else if (msg.dis < 1000 && msg.dis > 500) {
              pLevel = 3
              self.map.zoomLevel(pLevel)
            } else if (msg.dis < 500) {
              pLevel = 2
              self.map.zoomLevel(pLevel)
            }
          }
          if(object.startP.length>15){
            qrCodeData.sFI = object.allFoors[allPointsData[object.startP].FloorID];
          }else{
            qrCodeData.sFI = object.allFoors[findFlooridByPointid(object.startP)];
          }
          qrCodeData.sPI = object.startP;
          qrCodeData.ePI = object.endP.toString();
          qrCodeData.eFI = object.allFoors[findFlooridByPointid(object.endP)];
          qrCodeData.type = type;
          generateUrl(qrCodeData)
          var floorIDsReO = msg.floorIDs.reverse()
          setTimeout(() => {
            self.map.showFloors([floorIDsReO.slice(0, 1).toString()])
          }, 500)
        } else {
          // $('#loadings').css('display', 'none')
          $(".searchPathFailMsg").html("路线规划失败，请重试")
          $(".searchPathFailBox").fadeIn()
        }
      }, type)
    }
  } else {
    $(".searchPathFailMsg").html("寻路失败，起点或终点不存在")
    $(".searchPathFailBox").fadeIn()
  }

  // 寻路提示框出现后不可点击地图提示
  if($('.clickDetails').css('display') != 'none'){
    $('.mask').show();
	}
}

var dataFloorId = ''
var textFloorNo = ''
// 切换楼层模块列表按钮点击事件
$(".floorToggleMinUl").on("click", '#floorToggleUl_List button', function () {
  floorPathToggle=true;
  if(floorPathToggle){
    $('.pathInfo').fadeOut(500);
  }
  textFloorNo = $(this).attr('value')
  dataFloorId = $(this).attr('data')
  nowFloorToggleNo = $(this).attr("id")
  $(".floorToggleUl").slideUp()
  $(this).parent().removeClass().addClass('floorToggleUl_List_Click');
  $(this).parent().siblings().removeClass().addClass('floorToggleUl_List')
  $(this).parent().find("i").removeClass().addClass('floorToggleMinUl_iR');
  $(this).parent().siblings().find("i").removeClass().addClass('floorToggleMinUl_i')
  self.map.showFloors([dataFloorId])
  // console.log(dataO)
  mapPositonReset()
  // FloorDetection()
  // $(".clickDetails").hide()
  // $('.scale,.floorToggle,.floorToggleUl,.floorToggleMinUl').css('bottom', '.6rem');
  object.floorIDs = [dataFloorId]
  // $('.looking').hide().show().html('正在查看 ' + object.allFoors[dataFloorId] + '');
})

//在onSelected内的导航状态下 移动地图暂停导航内贴上以下代码块 [██████code block██████]
// isFloorToggleCounter = true;
// $(".floorToggleMinUl li button").removeAttr("disabled");

// 在继续导航内的 isPause == truen内调用
function continueToNavigateInside() {
  $(".floorToggleMinUl li").each(function (i, e) {
    if ('isMark' == $(e).attr('data')) {
      $(".floorToggleMinUl li").eq(i).removeClass().addClass('floorToggleUl_List-active')
      $(".floorToggleMinUl li").eq(i).siblings().show().removeClass().addClass('floorToggleUl_List')
      $(".floorToggleMinUl li").eq(i).find('i').removeClass().addClass("floorToggleMinUl_iR")
      $(".floorToggleMinUl li").eq(i).siblings().find('i').removeClass().addClass("floorToggleMinUl_i")
      $(".floorToggleMinUl li").eq(i).attr('data', 'isMark')
      $(".floorToggleMinUl li").eq(i).siblings().attr('data', ' ')
      self.map.showFloors([$(".floorToggleMinUl li").eq(i).find('button').attr('data')])
    }
  })
}
// 对象去重函数
function uniq(arr, name) {
  var hash = {};
  return arr.reduce(function (item, next) {
    hash[next[name]] ? '' : hash[next[name]] = true && item.push(next);
    return item;
  }, []);
}
// 在继续导航方法结尾贴上以下代码块 [██████code block██████]
// isFloorToggleCounter = false;
// floorCounter = 0;
// $(".floorToggleMinUl li button").attr("disabled", "true")

// 在模拟导航结束方法和取消方法调用
function moEndClear() {
  object.startP = '';
  object.endP = '';
  object.startPP = '';
  object.endPP = '';
  object.isFind = false
  mapPositonReset()
  // $(".floorToggle").show()
  $('.floorToggleUl,.selectionBox').hide()
  $(".floorToggleUl li").show()
  // $(".floorToggleMinUl li button").removeAttr("disabled");
  // $('.floorToggleMinUl').children().remove()
  if (nowFloorId) {
    $('.floorToggle').html("" + object.allFoors[nowFloorId] + "")
  }
  // $('.scale,.floorToggle,.floorToggleUl,.floorToggleMinUl').css('bottom', '.3rem');
}

// 在返回方法goback内调用
function goBackInside(params) {
  // $(".floorToggleMinUl li button").removeAttr("disabled");
  // $('.floorToggleMinUl').children().remove()
  object.startP = '';
  object.endP = '';
  object.startPP = '';
  object.endPP = '';
  object.xlId.startId = ''
  object.xlId.endId = ''
  nowFloorId = ''
  isStart = false;
  isEnd = false
  mapPositonReset()
  $(".lastChoice").find("li").eq(1).children().remove()
  $(".lastChoice").find("li").eq(1).html(" ")
  $(".lastChoice").find("li").eq(1).append("<span></span>")
  $(".lastChoice").find("li").eq(2).children().html("查看楼梯路线")

  $(".secondChoice").find("li").eq(1).children().remove()
  $(".secondChoice").find("li").eq(1).html(" ")
  $(".secondChoice").find("li").eq(1).append("<span></span>")
  $(".secondChoice").find("li").eq(2).children().html("查看扶梯路线")

  $(".firstChoice").find("li").eq(1).children().remove()
  $(".firstChoice").find("li").eq(1).html(" ")
  $(".firstChoice").find("li").eq(1).append("<span></span>")
  $(".firstChoice").find("li").eq(2).children().html("查看直梯路线")
  $(".pathChoice div").find("li").removeClass()

  $('.selection').hide()
  $('.selectionLoading').show()
}

// 起点终点置换方法内调用
function reverseInside() {

  // $('.floorToggleMinUl').children().remove()
  if (msgLong > 1) {
    var bbc
    bbc = fIndex;
    fIndex = lIndex;
    lIndex = bbc;
  }
  // $(".floorToggleMinUl li").css("display", "block")
}




$(".mogo").on('click', function () {

  if (pathFlag) {
    if ($('.mogo').text() === '点击进行模拟导航') {
      simulateNavigate();
      $('.pathInfo').hide();
      $('.mogo').css("background-image", "url('./images/stopMogo.png')");
      $('.mogo').text('点击结束模拟导航');
    } else if ($('.mogo').text() === '点击结束模拟导航') {
      goBack();
      $(".floorToggleMinUl").hide();
      $('.changeFBox').show()
      $('.mogo').css("background-image", "url('./images/mogo.png')");
      $('.mogo').text('点击进行模拟导航');
      $('.navigationCompletedPopUps').fadeIn(100,()=>{
        setTimeout(() => {
          $('.navigationCompletedPopUps').fadeOut(1000);
        }, 500);
      })
    }else if($('.mogo').text() === '继续进行模拟导航'){
      continueToNavigateInside()
      self.map.toggleSimulate(true);
      isPause = false;
      $('.mogo').css("background-image", "url('./images/stopMogo.png')");
      $('.mogo').text('点击结束模拟导航');
    }
  }


})